from odoo import fields, models, _
from odoo.tools import DEFAULT_SERVER_DATETIME_FORMAT
from odoo.tools.misc import format_datetime, format_date


class ReportStockInInternalOutWizard(models.TransientModel):
    _name = 's.report.stock.in.internal.out.wizard'
    _description = 'Report Stock In Internal Out Wizard'

    product_id = fields.Many2one('product.product', string='Product')
    product_category_id = fields.Many2one('product.category', string='Product Category')
    business_type = fields.Selection([
        ('sale', _('Sale')),
        ('purchase', _('Purchase')),
        ('material_scrap', _('material scrap')),
        ('finished_in', _('Finished product storage'))
    ], string='Business Type')
    start_date = fields.Datetime('start date')
    end_date = fields.Datetime('end date')

    def action_view(self):
        self.ensure_one()
        if self.start_date and self.end_date:
            start_date = min(self.start_date, self.end_date)
            end_date = max(self.start_date, self.end_date)
        else:
            start_date = self.start_date
            end_date = self.end_date
        return {
            'name': _('Report Stock In Internal Out'),
            'view_mode': 'tree',
            'res_model': 's.report.stock.in.internal.out',
            'type': 'ir.actions.act_window',
            'context': {
                'start_date': start_date,
                'end_date': end_date,
                'product_category_id': self.product_category_id.id,
                'product_id': self.product_id.id,
                'business_type': self.business_type,
            },
            'display_name': f'{format_date(self.env, start_date)} - {format_date(self.env, end_date)}',
            'target': 'current'
        }
